import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/udt_tb_workreportrecord.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/pages/common/date_time_select.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class ProductWorkreportrecordDialog extends StatefulWidget {
  ProductWorkreportrecordDialog();

  @override
  _ProductWorkreportrecordDialogState createState() =>
      _ProductWorkreportrecordDialogState();
}

class _ProductWorkreportrecordDialogState
    extends State<ProductWorkreportrecordDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  UdtTbWorkreportrecord udtTbWorkreportrecord = UdtTbWorkreportrecord();
  DateFormat format = DateFormat('yyyy-MM-dd HH:mm:ss');
  List<SelectOptionVO> workshopList = [];
  List<SelectOptionVO> teamList = [];

  @override
  void initState() {
    super.initState();
    udtTbWorkreportrecord.reportDateStart = Utils.getDate()+" 00:00:00";
    udtTbWorkreportrecord.reportDateEnd = Utils.getDate()+" 23:59:59";
    _loadSelectVO();
  }

  _loadSelectVO() async {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    if (userInfo.major != null) {
      workshopList = await DynamicDataApi.getModelSelect(
          model: "hr.department",
          name: "complete_name",
          key: "parent_id",
          operator: "!=",
          val: null);
    }
    teamList = await DynamicDataApi.getModelSelect(
        model: "base.team",
        name: "teamname",
        key: 'workshop_id',
        val: int.parse(userInfo.deptId!));
    udtTbWorkreportrecord.workshopId = userInfo.deptId;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();

    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          DateTimeSelect(
            context,
            label: '报工开始日期',
            value: udtTbWorkreportrecord.startDate,
            lastDate: isNotBlank(udtTbWorkreportrecord.endDate)
                ? format.parse(udtTbWorkreportrecord.endDate!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              udtTbWorkreportrecord.startDate = v;
              setState(() {});
            },
            onSaved: (v) {
              udtTbWorkreportrecord.startDate = v;
            },
          ),
          DateTimeSelect(
            context,
            label: '报工结束日期',
            value: udtTbWorkreportrecord.endDate,
            firstDate: isNotBlank(udtTbWorkreportrecord.startDate)
                ? format.parse(udtTbWorkreportrecord.startDate!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              udtTbWorkreportrecord.endDate = v;
              setState(() {});
            },
            onSaved: (v) {
              udtTbWorkreportrecord.endDate = v;
            },
          ),
          userInfo.major != null
              ? CrySelect(
                  label: '生产车间',
                  dataList: workshopList,
                  value: udtTbWorkreportrecord.workshopId,
                  onSaved: (v) {
                    udtTbWorkreportrecord.workshopId = v;
                  },
                  onChange: (v) async {
                    if (udtTbWorkreportrecord.workshopId != v) {
                      formKey.currentState!.save();
                      udtTbWorkreportrecord.workshopId = v;
                      try {
                        teamList = await DynamicDataApi.getModelSelect(
                            model: "base.team",
                            name: "teamname",
                            key: 'workshop_id',
                            val: int.parse(v));
                      } catch (e) {
                        teamList = [];
                      }
                      setState(() {});
                    }
                  },
                )
              : CryInput(
                  label: '生产车间',
                  enable: false,
                  value: userInfo.deptName,
                ),
          CrySelect(
            label: '班组',
            dataList: teamList,
            value: udtTbWorkreportrecord.teamId,
            onSaved: (v) {
              udtTbWorkreportrecord.teamId = v;
            },
          ),
          CryInput(
            label: '报工人',
            value: udtTbWorkreportrecord.reportworkerName,
            onSaved: (v) {
              udtTbWorkreportrecord.reportworkerName = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, udtTbWorkreportrecord);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
